package com.atguigu.gulimall.order.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.stereotype.Component;

@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016102600763662";

    // 商户(自己的)私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQC95wahyFO7gluN1+Vg2/IvUBtWc82aVSlZoDGkyf7Q9cXHpEcDYO/VYA9LFafC+ahBtocRp9D3hP7XIoItoOH3Ohjc6wFAb3S4g1MJz6geKYcuMhUIeOfdJWxnWDBYWVOXubLORycTLGkh3xNcqFH2Zr//G5n7JCObPQmh9Z42biCSUsj4tUBYvGGdcjuZQshunVTviaZE/YWXFAu744wQcDvvL8aUfl2J7Xlx6d0WiD6LnuFvTDzNkBMr5DSJcuRIiK4+1XGYKzOspFhwdENxI2skhnKVGcbODOubi+sOep/xd1LniNpPoTLmkKZ0xXx4lP71p91kwE6XYdMPSf99AgMBAAECggEBALD26Sc6F01VXRwBZK+uRzHv5InbwG1hjvcGYTy/P8ZaQcSZQdPUMasVqSq/M2ZuYG1cOuIpEIKYXSL99XGvyuMoMKMuHCx/sLWBK5CpAnGZIW/bG96ZTPi4NxzXAbZTB6MMWaQAillDuXTm/JQMBtUnSyPgBM4UD1pKtv9Gc/kjaXVAhyQUfqNlE3Q4UAnTsRQyAC952XwlgJRfb8BuSUn1l0VDPeeH+2TqP2dmp2uQyIm2kogoNSSeb9bDSrUA7TqorzHw6XS6gkeO5NxwmKHwZ6uUuaryMnclT2OHZofMSjuJnBi7xYF1rKeWKQ0mjXGMHeaNmznVtvgD4C9hCcECgYEA9zbTZYhgOEAdx/o8hEHajCTXVWLWFrWy166AhsdVTaoIxcu2szuuNBbn1K412AMnsk0pGIHaVMtuK1Bn59Z8zaU3r8uYZ3keLp5mo5xJHij7te0vKlsIl4VYKvYChxBFVtsFZ19Pc77YBdELK+VVprrzgIH1SXr7OwQd7U6cpG8CgYEAxKbGBsxNGy12VJXQW6Zl6NJyJPezUm9wQFgtjuHI40leRJi7Qj/n9Xv8TZGT5iXahyt7xQr6Iit/pgqfGhhAueag1MGS6Abjvkox/AIFOxGGCGXVnfEMCAKFG0k43crjlnOnaJrWFGsdFih9bYj//M7CoMeecycQ/pelcpUlCNMCgYEAg6epEUt9ordSpOt5Sx4fG7+LBigqx9FgO0jtndmOblv9bAk/dVh1P7xrHnLgrfeQLn61Qh31fm8QbvTVQTyTHpm8fFy6/3AbSMyG8N8MeYlt88cq9S2h97/F4qs9UVyy8LyXtiOmFoguV3qqrmfmI3MwtuF/FfDu1tEsdvMErPUCgYEApM/ZO61SGir5914v6AAHfR+TBqSKp9Y5z9OXKx7UDJla5kbS+m0w7K7i/+e4erTS92JcMwJGLQkjzHcubOJSrPs7Y9RwWQIdMUhhqAHRfHXtTXB7ngl9ueDSydGOVWRLZba8YZETRr2W/REmPf0J/kkTGUjz5TKirZ1Ozj9tcH0CgYEAuowK+J66W4NrqPsfESdiXFAyyH3h88A1D54KDn7dqLSrRcv1ZI4FJ8ncpqrKuSP/gaHMuA7oOEoelyWm4L8gXtVrO1o9qV6aSFfG2NB/4vB9JkrFpE1ZRPCf/Ui6iNijeiXpvdCN+M8AMVZhZCejJaMRLjtEGzZJMAniJsSuHPk=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhpo4qLIJ2u8CJTv7RcaB2yKQySRyembM/dtJvD+SctQ/wF6q5rRA2j4GiCqkC55D8cd4jdikqhSIClwjYrqnwFPgPebG7oKT/wFd0I1t0Tk0ll8AJGRUifNgW6sb8hMeBXYhzbMGIhBhG76PuuC86zjOtcy51r/asSXv4jrEAk0F9pmOfQQxul+m/bj4n4gpSxKA5I+mt8aSHAIX8sG7s7QvsnZ4TnBrl7Zdq1MRnMJvfi9f3DQLH9Q7ToiZjw130DPNdqbTHkgLZTXSJdVhKHPUwQorowAdkkway2Qv02S2g2RymWhpM2E6xDrAkIvXjUAyVk/Kw8p9LsffjpbXNwIDAQAB";

    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://manchao.utools.club/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo){

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
